package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.AgentsMapper;
import com.yxuan.springboot.mapper.OutRecordsMapper;
import com.yxuan.springboot.model.Agents;
import com.yxuan.springboot.util.Result;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yxuan.springboot.model.OutRecords;
import com.yxuan.springboot.service.OutRecordsService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author zchao
 *    OutRecords业务层实现类
 * @date 2024-05-24 20:58:16
 */
@Slf4j
@Service
@Transactional
public class OutRecordsServiceImpl extends BaseServiceImpl<OutRecords> implements OutRecordsService {

    @Autowired
    private OutRecordsMapper outRecordsMapper;
    @Autowired
    private AgentsMapper agentsMapper;
    /*
    * 查看提现金额
    * */

    @Override
    public Result getOutAmount(String agentId) throws Exception {

        BigDecimal outAmountOrder = outRecordsMapper.getOutAmountOrder(agentId);
        if (outAmountOrder==null){
            outAmountOrder = BigDecimal.ZERO;
        }
        Agents agents = new Agents();
        agents.setAgentid(agentId);
        Agents targetA = agentsMapper.queryObjectByPramas(agents);
        //判断该agentId用户是否为二级代理
        if (targetA.getUpId()!=null&&!targetA.getUpId().equals("")){
            //是二级代理
            Agents A2 = new Agents();
            A2.setAgentid(targetA.getUpId());
            Agents targetA2 = agentsMapper.queryObjectByPramas(A2);
            log.info("========"+outAmountOrder);
            log.info("++++++++"+targetA2);
            outAmountOrder = outAmountOrder.multiply(new BigDecimal(targetA2.getShareRatio())).setScale(2,RoundingMode.FLOOR);//向下舍入
        }else {
            //是一级代理
            //首先查该一级代理下直接订单的价格就是outAmountOrder
            //计算该一级代理可以从他底下所有二级代理那分得的钱
            Agents A3 = new Agents();
            A3.setUpId(agentId);
            List<Agents> As = agentsMapper.queryListByPramas(A3);
            if (As.size()!=0){
                //相加,等于0说明没有二级代理，所以一开始的outAmountOrder就是最终结果
                for (Agents a : As) {
                    //查该二级代理所有的订单金额
                    BigDecimal allOrderAmount = outRecordsMapper.getAllOrderAmount(a.getAgentid());
                    //计算该一级代理可以分到的钱(总金额*一级代理比例*(1-二级代理比例))
                    allOrderAmount = allOrderAmount.multiply(new BigDecimal(targetA.getShareRatio())).multiply(new BigDecimal(1-a.getShareRatio())).setScale(2,RoundingMode.FLOOR);
                    //将outAmountOrder与累加结果相加得出最终结果
                    outAmountOrder = outAmountOrder.add(allOrderAmount).setScale(2, RoundingMode.FLOOR);
                }
            }
        }

        BigDecimal outAmountRecord = outRecordsMapper.getOutAmountRecord(agentId);
        // 减去两个BigDecimal值
        BigDecimal netAmount = outAmountOrder.subtract(outAmountRecord);

        // 设置小数点后保留两位
        netAmount = netAmount.setScale(2, RoundingMode.FLOOR);

        return new Result(netAmount,200);
    }

    /*
     *添加提现记录
     */
    @Override
    public Result setOutRecord(String agentId) throws Exception {

        OutRecords outRecords = new OutRecords();
        BigDecimal outAmountOrder = outRecordsMapper.getOutAmountOrder(agentId);
        if (outAmountOrder==null){
            outAmountOrder = BigDecimal.ZERO;
        }
        Agents agents = new Agents();
        agents.setAgentid(agentId);
        Agents targetA = agentsMapper.queryObjectByPramas(agents);
        //判断该agentId用户是否为二级代理
        if (targetA.getUpId()!=null&&!targetA.getUpId().equals("")){
            //是二级代理
            Agents A2 = new Agents();
            A2.setAgentid(targetA.getUpId());
            Agents targetA2 = agentsMapper.queryObjectByPramas(A2);
            outAmountOrder = outAmountOrder.multiply(new BigDecimal(targetA2.getShareRatio())).setScale(2,RoundingMode.FLOOR);//向下舍入
        }else {
            //是一级代理
            //首先查该一级代理下直接订单的价格就是outAmountOrder
            //计算该一级代理可以从他底下所有二级代理那分得的钱
            Agents A3 = new Agents();
            A3.setUpId(agentId);
            List<Agents> As = agentsMapper.queryListByPramas(A3);
            if (As.size()!=0){
                //相加,等于0说明没有二级代理，所以一开始的outAmountOrder就是最终结果
                for (Agents a : As) {
                    //查该二级代理所有的订单金额
                    BigDecimal allOrderAmount = outRecordsMapper.getAllOrderAmount(a.getAgentid());
                    //计算该一级代理可以分到的钱(总金额*一级代理比例*(1-二级代理比例))
                    allOrderAmount = allOrderAmount.multiply(new BigDecimal(targetA.getShareRatio())).multiply(new BigDecimal(1-a.getShareRatio())).setScale(2,RoundingMode.FLOOR);
                    //将outAmountOrder与累加结果相加得出最终结果
                    outAmountOrder = outAmountOrder.add(allOrderAmount).setScale(2, RoundingMode.FLOOR);
                }
            }
        }

        BigDecimal outAmountRecord = outRecordsMapper.getOutAmountRecord(agentId);
        // 减去两个BigDecimal值
        BigDecimal netAmount = outAmountOrder.subtract(outAmountRecord);

        // 设置小数点后保留两位
        netAmount = netAmount.setScale(2, RoundingMode.FLOOR);
        // 检查outAmount是否等于0，0.0或0.00
        if (netAmount.compareTo(BigDecimal.ZERO) <= 0) {
            // 可提现金额为0，不让提现,不添加提现记录
            return new Result(201,"可提现金额为0，不让提现");
        }
        outRecords.setOutState("未提现");
        outRecords.setAgentid(agentId);
        outRecords.setPayout(netAmount);
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        Date AppliedTime = formatter.parse(formatter.format(date));
        outRecords.setOutTime(AppliedTime);
        outRecordsMapper.insert(outRecords);


        return new Result(200,"提现记录添加成功");
    }
}
